#include "sensorthresholdparametersset.h"
#include "ui_sensorthresholdparametersset.h"
Q_GLOBAL_STATIC(SensorThresholdParametersSet,theInstance);
SensorThresholdParametersSet::SensorThresholdParametersSet(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SensorThresholdParametersSet)
{
    ui->setupUi(this);
    this->setWindowTitle("报警阈值设置界面");
    this->setWindowIcon(QIcon(":res/res/ThresholdValueSet.ico"));
    ui->spinBoxUpperLimit->setRange(0,2000);
    ui->spinBoxLowerLimit->setRange(0,2000);
}

SensorThresholdParametersSet::~SensorThresholdParametersSet()
{
    delete ui;
}

SensorThresholdParametersSet *SensorThresholdParametersSet::instance()
{
    return theInstance;
}

void SensorThresholdParametersSet::displaySensorThresholdParametersSetTable()
{
    // 2. 创建模型并绑定表
         model = new QSqlTableModel(this,UserMysqlS::instance()->getDataBase());
         model->setTable("sensorthresholdparameters");

         // 3. 设置列标题（字段名到中文的映射）
         model->setHeaderData(0,Qt::Horizontal,"id");
         model->setHeaderData(1, Qt::Horizontal, "传感器");
         model->setHeaderData(2, Qt::Horizontal, "报警上限");
         model->setHeaderData(3, Qt::Horizontal, "报警下限");
         model->setHeaderData(4, Qt::Horizontal, "上警发生");
         model->setHeaderData(5, Qt::Horizontal, "下警发生");


         // 4. 加载数据
         if (!model->select()) {
             QMessageBox::critical(this, "错误", "查询失败: " + model->lastError().text());
             //UserMysqlS::instance()->getDataBase()->close();
             return;
         }

         // 5. 创建视图并绑定模型
        // QTableView *tableView = new QTableView(this);
         ui->tableViewSensorParametersSet->setModel(model);
         //tableView->setModel(model);
         ui->tableViewSensorParametersSet->resizeColumnsToContents();//自适应
         ui->tableViewSensorParametersSet->setSortingEnabled(true);//自动排序

         ui->tableViewSensorParametersSet->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//可拉伸

         // 6. 布局
 //        QVBoxLayout *layout = new QVBoxLayout(this);
 //        layout->addWidget( ui->tableViewSensorDisplay);
 //        setLayout(layout);

         // 注意：此处未关闭数据库连接，因为模型可能后续需要操作

}

void SensorThresholdParametersSet::on_pushButtonConfirm_clicked()
{

//获取当前的id
int id=ui->comboBoxSensor->currentIndex()+1;

//获取上限下限值
int UpperLimit=ui->spinBoxUpperLimit->value();
int LowerLimit=ui->spinBoxLowerLimit->value();
setSensorThresholdParameters(id,UpperLimit,LowerLimit);
}


void SensorThresholdParametersSet::on_pushButtonExit_clicked()
{
    this->close();
}


void SensorThresholdParametersSet::on_pushButtonRefresh_clicked()
{
    //禁用按钮防止重复点击
            ui->pushButtonRefresh->setEnabled(false);
            QApplication::processEvents();  // 立即更新UI

            // 重新加载数据
            if (!model->select()) {
                QMessageBox::critical(this, "错误", "刷新失败: " + model->lastError().text());
            } else {
                // 滚动到最新数据（假设按时间降序排序）
                ui->tableViewSensorParametersSet->scrollToTop();
            }

            // 恢复按钮状态
            ui->pushButtonRefresh->setEnabled(true);

}

void SensorThresholdParametersSet::setSensorThresholdParameters(int id, int UpperLimit, int LowerLimit)
{
    //获取未改变之前的上下限参数
    int LastUpperLimit=0;
    int lastLowerLimit=0;
    LastUpperLimit=UserMysqlS::instance()->getSensorThresholdParameters(id,"报警上限");
    lastLowerLimit=UserMysqlS::instance()->getSensorThresholdParameters(id,"报警下限");
    if(UpperLimit!=LastUpperLimit)
    {
        //设置新值
       if(UserMysqlS::instance()->setSensorThresholdParameters(id,"报警上限",UpperLimit))
       {
           QMessageBox::information(this,"success","更新上限成功");
       }
      else
       {
           QMessageBox::information(this,"failed","更新上限失败");
       }

    }
   if(LowerLimit!=LastUpperLimit)
   {
       //设置新值
       if(UserMysqlS::instance()->setSensorThresholdParameters(id,"报警下限",LowerLimit))
       {
           QMessageBox::information(this,"success","更新下限成功");
       }
       else
       {

       QMessageBox::information(this,"failed","更新下限失败");

       }

   }

  qDebug()<<"LastUpperLimit"<<LastUpperLimit;


}


void SensorThresholdParametersSet::on_comboBoxSensor_highlighted(int index)
{
//获取当前的上下限
    index++;
    int  currentUpperLimit=UserMysqlS::instance()->getSensorThresholdParameters(index,"报警上限");
    int currentLowerLimit=UserMysqlS::instance()->getSensorThresholdParameters(index,"报警下限");
    ui->spinBoxLowerLimit->setValue(currentLowerLimit);
    ui->spinBoxUpperLimit->setValue(currentUpperLimit);
}

